package com.tianqu.tms.www.tradeStatistics.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.MarketCarTransferInfo;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.tradeStatistics.viewhelper.TradeStatisticsViewhelper;

/**
 * @author zhangxueliang
 * 车辆名称补全录入controller
 */
public class SpecialCarNameInputController extends TmsController{
	private Logger logger = Logger.getLogger(SpecialCarNameInputController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			controller.begin();
			TransferAcceptedInfo transferAcceptedInfo = new TransferAcceptedInfo();
			TradeStatisticsViewhelper view = new TradeStatisticsViewhelper();
			String serial = (String)uc.getAttribute("serial");
			transferAcceptedInfo = transferAcceptedInfo.getTransferBybasicCarId(serial);
			if(!StringUtil.isNullOrBlank(transferAcceptedInfo.getSerial())){
				if(transferAcceptedInfo.getMarketType() == 1){//库存
					Car car = Car.getInstance();
					car = car.findCarBySerial(serial);
					if(car != null){
						view.setSerial(serial);
						view.setModelName(car.getVendorCode() + "-" +car.getModelCode());
						view.setInsertTime(DateUtil.DateToString11(car.getInsertTime()));
						view.setMarketType("库存");
						// 车辆品牌
						if(!StringUtil.isNullOrBlank(car.getVendorId())){
							view.setVendorId(car.getVendorId());
							view.setBrandId(car.getBrandId());
							view.setCarStyleId(car.getCarStyleId());
							if("V".equals(car.getVendorId().substring(0,1))){
								view.setFromType(2);
							
							}else if("S".equals(car.getVendorId().substring(0,1))){
								view.setFromType(1);
								
							}
							view.setDisplacement(Integer.toString(car.getDisplacement()));
							view.setGearbox(Integer.toString(car.getGearbox()));
						}
					}
				}else if(transferAcceptedInfo.getMarketType() == 2){//非库存
					TransferPersonalCarInfo pCar = TransferPersonalCarInfo.getInstance();
					pCar = pCar.getPersonalCarInfoBySerial(serial);
					// 车辆品牌
					if(!StringUtil.isNullOrBlank(pCar.getVendorId())){
						view.setVendorId(pCar.getVendorId());
						view.setBrandId(pCar.getBrandId());
						view.setCarStyleId(pCar.getCarStyleId());
						if("V".equals(pCar.getVendorId().substring(0,1))){
							view.setFromType(2);
						
						}else if("S".equals(pCar.getVendorId().substring(0,1))){
							view.setFromType(1);
							
						}
						view.setDisplacement(Integer.toString(pCar.getDisplacement()));
						view.setGearbox(Integer.toString(pCar.getGearbox()));
					}
					
					view.setSerial(serial);
					view.setModelName(pCar.getVendorCode() + "-" +pCar.getModelCode());
					view.setInsertTime(DateUtil.getDateByTimeStamp(pCar.getInsertTime()));
					view.setMarketType("非库存");
				}
				view.setVin(transferAcceptedInfo.getVin());
				view.setTansferSerial(transferAcceptedInfo.getSerial());
				view.setLicensePlate(transferAcceptedInfo.getLicensePlate());
				Codes code = Codes.getInstance();
				view.setTransferStatus(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS,transferAcceptedInfo.getStatus()));
				
			}else{
				Car car = Car.getInstance();
				car = car.findCarBySerial(serial);
				if(car.getSerial() != null && car.getSerial() != ""){
					view.setSerial(serial);
					view.setModelName(car.getVendorCode() + "-" +car.getModelCode());
					view.setInsertTime(DateUtil.DateToString11(car.getInsertTime()));
					view.setMarketType("库存");
					view.setVin(car.getVin());
					view.setLicensePlate(car.getLicensePlate());
					// 车辆品牌
					if(!StringUtil.isNullOrBlank(car.getVendorId())){
						view.setVendorId(car.getVendorId());
						view.setBrandId(car.getBrandId());
						view.setCarStyleId(car.getCarStyleId());
						if("V".equals(car.getVendorId().substring(0,1))){
							view.setFromType(2);
						
						}else if("S".equals(car.getVendorId().substring(0,1))){
							view.setFromType(1);
							
						}
						view.setDisplacement(Integer.toString(car.getDisplacement()));
						view.setGearbox(Integer.toString(car.getGearbox()));
					}
				}else{
					MarketCarTransferInfo marketCarTransferInfo = new MarketCarTransferInfo();
					marketCarTransferInfo = marketCarTransferInfo.findMarketCarTransferInfo(serial);
					view.setSerial(marketCarTransferInfo.getSerial().trim());
					view.setTansferSerial(marketCarTransferInfo.getTransferSerial().trim());
					view.setLicensePlate(marketCarTransferInfo.getLicensePlate().trim());
					view.setVin(marketCarTransferInfo.getVin());
					view.setInsertTime(DateUtil.getDateByTimeStamp(marketCarTransferInfo.getAcceptTime()));
					view.setModelName(marketCarTransferInfo.getVendorCode().trim() + "-" +marketCarTransferInfo.getModelCode().trim());
					view.setMarketType("老数据");
					// 车辆品牌
					if(!StringUtil.isNullOrBlank(marketCarTransferInfo.getVendorId())){
						view.setVendorId(marketCarTransferInfo.getVendorId());
						view.setBrandId(marketCarTransferInfo.getBrandId());
						view.setCarStyleId(marketCarTransferInfo.getCarStyleId());
						if("V".equals(marketCarTransferInfo.getVendorId().substring(0,1))){
							view.setFromType(2);
						
						}else if("S".equals(marketCarTransferInfo.getVendorId().substring(0,1))){
							view.setFromType(1);
							
						}
						view.setDisplacement(Integer.toString(marketCarTransferInfo.getDidisplacement()));
						view.setGearbox(Integer.toString(marketCarTransferInfo.getGearbox()));
					}
				}
			}
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
